<?php

class AdvertisingController extends Controller {    
    public $metaDescription = null;
    public $metaKeywords = null;
    public $linkCanoncical = null;
    public $title = null;
    public $layout = 'column_other';

    public function actionIndex(){ 
        $url = new Url();                             
        $page = isset($_GET['page']) ? intval($_GET ['page']):1;    
        $this->metaDescription = "Chương trình khuyến mại chào xuân";
        $this->metaKeywords = "Chuong trinh khuyen mai chao xuan";
        $this->title = "Chuong trinh khuyen mai chao xuan";       
        $this->linkCanoncical = $url->createUrl("advertising/index")."?page=".$page;
        $rows_per_page = 15;            
        $data = AdminKhuyenmai::getAllRowsByKhuyenmai();        
        $count = count($data);
        if($count % $rows_per_page == 0)
        {
            $totalpage = floor($count/$rows_per_page);
        }
        else
        {
            $totalpage = floor($count/$rows_per_page) + 1;                             
        }            
        $begin = ($page - 1)*$rows_per_page;
        $end = $page * $rows_per_page;                    
        $util = new Paging();
        $data = AdminKhuyenmai::getRowsByKhuyenmaiPaging($begin,$end);
        $page = $util->showPageNavigation($page,$totalpage,'index?');
        $this->render("index",array('data'=>$data,'page'=>$page));        

    }
    
    public function actionAdd(){        
        if(isset($_GET['edit']))
        {         
            $edit = 1;
            $id = $_GET['edit'];
            $model = new AdminKhuyenmai();
            $data =  $model->getRowsById($id);                           
            $this->render("add",array('edit'=>$edit,'data'=>$data));
        }
        else
        {
            $edit = 0;
            $this->render("add",array('edit'=>$edit));   
        }        
    }
    
    public function actionSubmitEdit(){                            
        $id = $_POST['id'];                     
        $str = "add?edit=".$id;                        
        echo $str;        
    }
    
    public function actionSubmitDelete(){                            
        $id = $_POST['id'];                                
        $model = new AdminKhuyenmai();
        $data = $model->deleteRowById($id);        
        if(count($data)==0)
        {
            echo 0;
        }
        else
        {
            echo 1;
        }
    }

    public function actionSubmitInsert(){        
        $ten = $_POST['ten'];                                
        $giai = $_POST['giai'];               
        $diachi = $_POST['diachi'];               
        $dienthoai = $_POST['dienthoai'];
        $date_win = DateUtils::convertDatedb($_POST['date_win']);
        $model = new AdminKhuyenmai();
        $data = $model->insertRow($ten,$giai,$diachi,$dienthoai,$date_win);
        if(count($data)==0)
        {
            echo 0;
        }
        else
        {
            echo 1;
        }
    }        
    
    public function actionSubmitUpdate(){                 
        $id = $_POST['id'];                                
        $ten = $_POST['ten'];                                
        $giai = $_POST['giai'];               
        $diachi = $_POST['diachi'];               
        $dienthoai = $_POST['dienthoai'];
        $date_win = DateUtils::convertDatedb($_POST['date_win']);
        $model = new AdminKhuyenmai();
        $data = $model->updateRowById($id,$ten,$giai,$diachi,$dienthoai,$date_win);
        if(count($data)==0)
        {
            echo 0;
        }
        else
        {
            echo 1;
        }
    } 
    
}
